<?php

namespace Rebecca\RebeccaBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;

use Rebecca\RebeccaBundle\Entity\PeriodosLaborales;
use Rebecca\RebeccaBundle\Form\PeriodosLaboralesType;

/**
 * PeriodosLaborales controller.
 *
 */
class PeriodosLaboralesController extends Controller
{
    /**
     * Lists all PeriodosLaborales entities.
     *
     */
    public function indexAction()
    {
        /* @var $component Rebecca\RebeccaBundle\Component\PeriodosLaboralesComponent */
        $component = $this->get("components.PeriodosLaborales");
        $entities = $component->getAll();

        return $this->render('RebeccaBundle:PeriodosLaborales:index.html.twig', array(
            'entities' => $entities
        ));
    }

    /**
     * Finds and displays a PeriodosLaborales entity.
     *
     */
    public function showAction($id)
    {
        /* @var $component Rebecca\RebeccaBundle\Component\PeriodosLaboralesComponent */
        $component = $this->get("components.PeriodosLaborales");

        $entity = $component->get($id);

        if (!$entity) {
            throw $this->createNotFoundException('Unable to find PeriodosLaborales entity.');
        }

        $deleteForm = $this->createDeleteForm($id);

        return $this->render('RebeccaBundle:PeriodosLaborales:show.html.twig', array(
            'entity'      => $entity,
            'delete_form' => $deleteForm->createView(),
        ));
    }

    /**
     * Displays a form to create a new PeriodosLaborales entity.
     *
     */
    public function newAction()
    {
        $entity = new PeriodosLaborales();
        $form   = $this->createForm(new PeriodosLaboralesType(), $entity);

        return $this->render('RebeccaBundle:PeriodosLaborales:new.html.twig', array(
            'entity' => $entity,
            'form'   => $form->createView()
        ));
    }

    /**
     * Creates a new PeriodosLaborales entity.
     *
     */
    public function createAction()
    {
        $entity  = new PeriodosLaborales();
        $request = $this->getRequest();
        $form    = $this->createForm(new PeriodosLaboralesType(), $entity);
        $form->bindRequest($request);

        if ($form->isValid()) {
            /* @var $component Rebecca\RebeccaBundle\Component\PeriodosLaboralesComponent */
            $component = $this->get("components.PeriodosLaborales");
            $component->save($entity);
            $component->commit();

            return $this->redirect($this->generateUrl('periodoslaborales'));
            
        }

        return $this->render('RebeccaBundle:PeriodosLaborales:new.html.twig', array(
            'entity' => $entity,
            'form'   => $form->createView()
        ));
    }

    /**
     * Displays a form to edit an existing PeriodosLaborales entity.
     *
     */
    public function editAction($id)
    {
        /* @var $component Rebecca\RebeccaBundle\Component\PeriodosLaboralesComponent */
        $component = $this->get("components.PeriodosLaborales");
        $entity = $component->get($id);

        if (!$entity) {
            throw $this->createNotFoundException('Unable to find PeriodosLaborales entity.');
        }

        $editForm = $this->createForm(new PeriodosLaboralesType(), $entity);
        $deleteForm = $this->createDeleteForm($id);

        return $this->render('RebeccaBundle:PeriodosLaborales:edit.html.twig', array(
            'entity'      => $entity,
            'edit_form'   => $editForm->createView(),
            'delete_form' => $deleteForm->createView(),
        ));
    }

    /**
     * Edits an existing PeriodosLaborales entity.
     *
     */
    public function updateAction($id)
    {
        /* @var $component Rebecca\RebeccaBundle\Component\PeriodosLaboralesComponent */
        $component = $this->get("components.PeriodosLaborales");

        $entity = $component->get($id);

        if (!$entity) {
            throw $this->createNotFoundException('Unable to find PeriodosLaborales entity.');
        }

        $editForm   = $this->createForm(new PeriodosLaboralesType(), $entity);
        $deleteForm = $this->createDeleteForm($id);

        $request = $this->getRequest();

        $editForm->bindRequest($request);

        if ($editForm->isValid()) {
            $component->update($entity);
            $component->commit();

            return $this->redirect($this->generateUrl('periodoslaborales'));
        }

        return $this->render('RebeccaBundle:PeriodosLaborales:edit.html.twig', array(
            'entity'      => $entity,
            'edit_form'   => $editForm->createView(),
            'delete_form' => $deleteForm->createView(),
        ));
    }

    /**
     * Deletes a PeriodosLaborales entity.
     *
     */
    public function deleteAction($id)
    {
        $form = $this->createDeleteForm($id);
        $request = $this->getRequest();

        $form->bindRequest($request);

        if ($form->isValid()) {
            /* @var $component Rebecca\RebeccaBundle\Component\PeriodosLaboralesComponent */
            $component = $this->get("components.PeriodosLaborales");
            $entity = $component->get($id);

            if (!$entity) {
                throw $this->createNotFoundException('Unable to find PeriodosLaborales entity.');
            }

            $component->delete($entity);
            $component->commit();
        }

        return $this->redirect($this->generateUrl('periodoslaborales'));
    }

    private function createDeleteForm($id)
    {
        return $this->createFormBuilder(array('id' => $id))
            ->add('id', 'hidden')
            ->getForm()
        ;
    }
    
    public function asignarContratoAction($id)
    {
        /* @var $component Rebecca\RebeccaBundle\Component\PeriodosLaboralesComponent */
        $component = $this->get("components.PeriodosLaborales");
        
        $tiposNominas = $component->getAllTiposNominas();
        
        $idTipoNomina = $this->getRequest()->get("idTipoNomina");
        
        $hayTipoNomina = false;
        $contratos = null;
        $tipoNominaActual = null;
        
        if (isset($idTipoNomina)){
            $contratos = $component->getContratos($idTipoNomina);    
            $tipoNominaActual = $component->getTipoNomina($idTipoNomina);
            $hayTipoNomina = true;
        }

        $entity = $component->get($id);

        if (!$entity) {
            throw $this->createNotFoundException('Unable to find PeriodosLaborales entity.');
        }

        return $this->render('RebeccaBundle:PeriodosLaborales:asignarcontrato.html.twig', array(
            'entity'          => $entity,
            'tiposNominas'    => $tiposNominas,
            'contratos'       => $contratos,
            'tipoNominaActual'=> $tipoNominaActual,
            'hayTipoNomina'   => $hayTipoNomina,
        ));
    }
    
    public function guardarasignacionAction()
    {
        /* @var $component Rebecca\RebeccaBundle\Component\PeriodosLaboralesComponent */
        $component = $this->get("components.PeriodosLaborales");
        
        $request = $this->getRequest();
        
        $allContratos = $request->get('AllContratos');
        $idTipoNomina = $request->get('idTipoNominaActual');
        $idPeriodoLaboral = $request->get('idPeriodoLaboral');
        
        $contratos = array();
        
        if (isset($allContratos)){            
            foreach ($component->getContratos($idTipoNomina) as $contrato){
                array_push($contratos, $contrato->getId());
            }
        }else{
            $contratos = $request->get('contratos');
        }
        
        $component->addPeriodosLaboralesContratos($contratos, $idPeriodoLaboral);

        return $this->redirect($this->generateUrl('periodoslaborales'));
    }
    
}
